# -*- coding: utf-8 -*- 
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/8/24
# @File : TPM（step2）F - Segments with Small Spread（CF）.py

from collections import deque
max_value = float('inf')
min_value = float('-inf')
class stk:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.que = deque()
        self.max_dp = deque()
        self.min_dp = deque()
        self.max_dp.append(min_value)
        self.min_dp.append(max_value)

    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """
        self.que.append(x)
        temp_max = self.max_dp[-1]
        temp_min = self.min_dp[-1]
        self.max_dp.append(max(temp_max,x))#保持最新的结果（最大和最小值）在栈顶
        self.min_dp.append(min(temp_min,x))

    def pop(self) -> None:
        """
        Removes the element from in front of queue and returns that element.
        """
        tmp = self.que.pop()
        tmp2 = self.max_dp.pop()
        tmp3 = self.min_dp.pop()

    def peek(self) -> int:
        """
        Get the front element.
        """
        return self.que[-1]

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return not self.que
    def get_max(self) -> int:
        """

        """
        return self.max_dp[-1]
    def get_min(self) -> int:
        """

        """
        return self.min_dp[-1]

def query(s1,s2):
    """
    :param s1:
    :param s2:
    :return: 一个区间的max等于这个区间分成前后两部分的max的结果再做一次max
    """
    dp1_max = s1.get_max()
    dp2_max = s2.get_max()
    dp1_min = s1.get_min()
    dp2_min = s2.get_min()

    return max(dp1_max,dp2_max)-min(dp1_min,dp2_min)

def remove(s1,s2):
    if(s1.empty()):
        while(not s2.empty()):
            s1.push(s2.peek())
            s2.pop()
    s1.pop()

def TPM():
    s1 = stk()
    s2 = stk()
    l = 0
    res = 0
    for r in range(n):
        s2.push(a[r])
        while(l <= r and query(s1,s2) > k):#这里也可以while(query(l,r)>k):因为没有元素是max为float('-inf')，min为float('inf')，所以相减是个负数。
            remove(s1,s2)
            l += 1
        res += (r-l+1)
    return res



if __name__ == '__main__':
    n, k = map(int,input().split())
    a = [int(i) for i in input().split()]
    ans = TPM()
    print(ans)
"""
Python 没有像multiset的数据结构，有heapq但是这道题实现不了在O(logn)的时间里删除某个元素（不知道下标只知道元素）
用min max又会超时

所以这道题用了C++，multiset可以s.erase(s.find(a[l++]));和取头和尾就是min和max，s.rbegin()-*s.begin()
不同于C++的priority_queue<Type, Container, Functional>
heapq和priority_queue才是对应
"""

"""

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=3e5+5;
int mp[maxm];
int a[maxm];
int n,k;
void solve(){
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    multiset<int>s;
    int l=1;
    int ans=0;
    for(int i=1;i<=n;i++){
        s.insert(a[i]);
        while(*s.rbegin()-*s.begin()>k){
            s.erase(s.find(a[l++]));
        }
        ans+=i-l+1;
    }
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    solve();
    return 0;
}

"""